#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "telephoneBook.h"
#include "contactPerson.h"

#define MAX_SIZE 32

enum Option
{
    新增联系人 = 1,
    查找联系人,
    模糊查找联系人,
    删除联系人,
    修改联系人,
    展示联系人,
    退出
};

enum Option2
{
    按姓名 = 1,
    按号码
};

void clearScreen()
{
    system("clear");
}

void displayMenu()
{
    printf("欢迎使用通讯录程序\n");
    printf("1. 新增联系人\n");
    printf("2. 查找联系人\n");
    printf("3. 模糊查找联系人\n");
    printf("4. 删除联系人\n");
    printf("5. 修改联系人\n");
    printf("6. 展示联系人\n");
    printf("7. 退出\n");
}

void displayMenu2()
{
    printf("请选择相应功能进行查询\n");
    printf("1. 按姓名查找联系人\n");
    printf("2. 按号码查找联系人\n");
}

void displayMenu3()
{
    printf("请选择相应功能进行查询\n");
    printf("1. 按姓名删除联系人\n");
    printf("2. 按号码删除联系人\n");
}

void displayMenu4()
{
    printf("请选择相应功能进行查询\n");
    printf("1. 按姓名排序展示联系人\n");
    printf("2. 按号码排序展示联系人\n");
}

int main(int argc, char **argv)
{
    TBook *a = initTBook(10, freeCp);
    ReadFromBook(a, Create);

    char *name = (char *)malloc(sizeof(char) * MAX_SIZE);
    char *phone = (char *)malloc(sizeof(char) * MAX_SIZE);

    if (NULL == name || NULL == phone)
    {
        perror("malloc error!\n");
        exit(1);
    }

    int choice = 0;

    while (1)
    {
        clearScreen();

        displayMenu();

        printf("请输入您的选择: ");
        scanf("%d", &choice);
        getchar();

        clearScreen();

        switch (choice)
        {
        case 新增联系人:
        {
            printf("您选择了新增联系人功能。\n");

            printf("请输入联系人姓名: \n");
            scanf("%s", name);

            printf("请输入联系人号码: \n");
            scanf("%s", phone);

            insertContact(a, createCper(name, phone));
            break;
        }

        case 查找联系人:
        {
            printf("您选择了查找联系人功能。\n");

            displayMenu2();

            int branch = 0;
            printf("请输入您的选择: ");
            scanf("%d", &branch);
            getchar();

            clearScreen();

            switch (branch)
            {
            case 按姓名:
            {
                printf("请输入联系人姓名: \n");
                scanf("%s", name);
                getchar();

                Cper *c = findContactPerson(a, name, isNameEqual);
                //Cper *c = binarySerchContactPerson(a, name, serachByName);
                if (NULL == c)
                {
                    printf("未找到相关信息!\n");
                    break;
                }
                else
                {
                    printCper(c);
                    break;
                }
            }

            case 按号码:
            {
                printf("请输入联系人号码: \n");
                scanf("%s", phone);
                getchar();

                //Cper *c = findContactPerson(a, phone, isPhoneEqual);
                Cper *c = binarySerchContactPerson(a, phone, serachByPhone);
                if (NULL == c)
                {
                    printf("未找到相关信息!\n");
                    break;
                }
                else
                {
                    printCper(c);
                    break;
                }
            }

            default:
            {
                printf("无效输入，请重新输入!\n");
                getchar();
                break;
            }
            }

            break;
        }

        case 模糊查找联系人:
        {
            printf("您选择了模糊查找联系人功能。\n");

            displayMenu2();

            int branch = 0;
            printf("请输入您的选择: ");
            scanf("%d", &branch);
            getchar();

            clearScreen();

            switch (branch)
            {
            case 按姓名:
            {
                printf("请输入联系人部分姓名: \n");
                scanf("%s", name);
                getchar();

                printAllMatches(a, name, isNameContaining, sortByName);
                break;
            }

            case 按号码:
            {
                printf("请输入联系人部分号码: \n");
                scanf("%s", phone);
                getchar();

                printAllMatches(a, phone, isPhoneContaining, sortByPhone);
                break;
            }

            default:
            {
                printf("无效输入，请重新输入!\n");
                getchar();
                break;
            }
            }

            break;
        }

        case 删除联系人:
        {
            printf("您选择了删除联系人功能。\n");

            displayMenu3();

            int branch = 0;
            printf("请输入您的选择: ");
            scanf("%d", &branch);
            getchar();

            clearScreen();

            switch (branch)
            {
            case 按姓名:
            {
                printf("请输入联系人姓名: \n");
                scanf("%s", name);
                getchar();

                Cper *c = findContactPerson(a, name, isNameEqual);
                if (NULL == c)
                {
                    printf("未找到相关信息!\n");
                    break;
                }
                else
                {
                    delContactPerson(a, name, isNameEqual);

                    printf("删除成功!\n");
                    break;
                }
            }

            case 按号码:
            {
                printf("请输入联系人号码: \n");
                scanf("%s", phone);
                getchar();

                Cper *c = findContactPerson(a, phone, isPhoneEqual);
                if (NULL == c)
                {
                    printf("未找到相关信息!\n");
                    break;
                }
                else
                {
                    delContactPerson(a, phone, isPhoneEqual);

                    printf("删除成功!\n");
                    break;
                }
            }

            default:
            {
                printf("无效输入，请重新输入!\n");
                getchar();
                break;
            }
            }

            break;
        }

        case 修改联系人:
        {
            printf("请输入待修改联系人姓名: \n");
            scanf("%s", name);
            getchar();

            Cper *c = findContactPerson(a, name, isNameEqual);
            if (NULL == c)
            {
                printf("未找到相关信息!\n");
                break;
            }
            else
            {
                char *newName = (char *)malloc(sizeof(char) * MAX_SIZE);
                char *newPhone = (char *)malloc(sizeof(char) * MAX_SIZE);
                if (NULL == newName || NULL == newPhone)
                {
                    perror("malloc error!\n");
                    exit(1);
                }

                printf("请输入新的姓名: \n");
                scanf("%s", newName);
                printf("请输入新的号码: \n");
                scanf("%s", newPhone);

                modifyContact(a, name, newName, newPhone);

                free(newName);
                free(newPhone);
                break;
            }
        }

        case 展示联系人:
        {
            printf("您选择了展示联系人功能。\n");

            displayMenu4();

            int branch = 0;
            printf("请输入您的选择: ");
            scanf("%d", &branch);
            getchar();

            clearScreen();

            switch (branch)
            {
            case 按姓名:
            {
                insertSort(a, sortByName);
                printTBook(a, printCper);
                break;
            }

            case 按号码:
            {
                insertSort(a, sortByPhone);
                printTBook(a, printCper);
                break;
            }

            default:
            {
                printf("无效输入，请重新输入!\n");
                getchar();
                break;
            }
            }

            break;
        }

        case 退出:
        {
            printf("感谢使用，再见！\n");
            SaveBook(a, GetInfo);
            free(name);
            free(phone);
            freeTBook(a);

            return 0;
        }

        default:
        {
            printf("无效输入，请重新输入!\n");
            getchar();
            break;
        }
        }
        SaveBook(a,GetInfo);
        getchar();
    }
}